<template>
  <a-modal
    width="600px"
    :footer="null"
    :maskClosable="false"
    :closable="false"
    :mask="false"
    v-model:open="modalShow"
    :wrap-style="{ overflow: 'hidden' }"
  >
    <template #title>
      <modalTitle @close="close" :title="'水位监测'"></modalTitle>
    </template>
    <div style="padding: 0 10px 10px 10px">
      <div class="content">
        <div style="display: flex; color: #ffffff99">
          <div style="font-size: 13px">{{ data }}</div>
          <div style="font-size: 13px; margin-left: 20px">现场指挥官：{{ spl.commander }}</div>
          <div style="margin-left: 20px; font-size: 13px">联系方式：{{ spl.commander_phone }}</div>
        </div>
        <div style="margin: 10px 0; font-weight: bold">实时监控</div>
        <div class="video-container" v-if="modalShow">
          <video v-if="isOK" ref="videoElementsw" class="video-js vjs-big-play-centered" autoplay controls></video>
          <div v-else class="jkbox-err">连接失败</div>
        </div>
        <div style="margin: 10px 0; font-weight: bold">水位监测</div>
        <div id="swlineChart" style="height: 180px; width: 100%"></div>
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { reactive, ref, nextTick } from "vue"
import * as echarts from "echarts"
import modalTitle from "../../modalTitle.vue"
import axios from "axios"
import videojs from "video.js"
import "video.js/dist/video-js.css"
const modalShow = ref(false)

const close = () => {
  modalShow.value = false
  if (player.value) {
    player.value.dispose()
  }
}
const data = ref("")

const device_no = ref("")
// 视频流
const spl = ref("")
const player = ref(null)
const videoElementsw = ref(null)
const isOK = ref(true)

const show = (e) => {
  data.value = e.data
  device_no.value = e.device_no
  modalShow.value = true
  isOK.value = true
  getSp()
  nextTick().then(() => {
    initLineChart()
  })
}

const initLineChart = () => {
  const dom = document.getElementById("swlineChart")
  const myChart = echarts.init(dom) // 初始化echarts实例
  const option = {
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "cross",
        label: {
          backgroundColor: "#6a7985"
        }
      }
    },
    legend: {
      show: false
    },
    grid: {
      top: "10%",
      left: "3%",
      right: "5%",
      bottom: "3%",
      containLabel: true
    },
    xAxis: [
      {
        type: "category",
        boundaryGap: false,
        data: ["23:34", "15:02", "15:19", "10:14", "03:14", "02:32", "02:25"],
        axisLabel: {
          show: true,
          interval: 0,
          textStyle: {
            color: "rgba(219, 225, 255, 1)"
          },
          formatter: function (params) {
            let newParamsName = ""
            const paramsNameNumber = params.length
            const provideNumber = 4
            const rowNumber = Math.ceil(paramsNameNumber / provideNumber)
            if (paramsNameNumber > provideNumber) {
              for (let p = 0; p < rowNumber; p++) {
                let tempStr = ""
                const start = p * provideNumber
                const end = start + provideNumber
                if (p === rowNumber - 1) {
                  tempStr = params.substring(start, paramsNameNumber)
                } else {
                  tempStr = params.substring(start, end) + "\n"
                }
                newParamsName += tempStr
              }
            } else {
              newParamsName = params
            }
            return newParamsName
          }
        }
      }
    ],
    yAxis: [
      {
        type: "value",
        splitNumber: 4,
        splitLine: {
          show: false
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: "#BDBDBD",
            width: 1,
            type: "dotted"
          }
        },
        axisLabel: {
          // 设置y轴的文字的样式
          textStyle: {
            show: true,
            color: "#BDBDBD",
            fontSize: "12"
          }
        }
      }
    ],
    series: [
      {
        name: "实时水位",
        type: "line",
        areaStyle: {},
        smooth: true,
        data: [12, 26, 91, 67, 39, 25, 80]
      },
      {
        name: "平均水位",
        type: "line",
        areaStyle: {},
        smooth: true,
        data: [76, 39, 64, 98, 35, 75, 58]
      }
    ]
  }
  myChart.setOption(option)
}
defineExpose({ show, close })

const play = (url) => {
  player.value = videojs(videoElementsw.value, {
    autoplay: true,
    muted: true,
    controls: true,
    sources: [
      {
        src: url,
        type: "application/x-mpegURL"
      }
    ]
  })

  player.value.on("error", () => {
    isOK.value = false
  })
}

const getSp = async () => {
  axios({
    method: "post",
    url: "http://ylz.chuangxing.ren/api/index/ics_test",
    data: {
      cameraIndexCode: device_no.value
    }
  }).then((res) => {
    spl.value = res.data.data.data
    play(spl.value.url)
  })
}
</script>
<style scoped>
.content {
  /* background: #4DDECF1A; */
  padding: 0 0 10px 0;
  /* border: 1px solid #4DDECF33; */
  /* border-radius: 10px; */
  color: #ffffff;
}

.btn {
  background: linear-gradient(180deg, rgba(0, 36, 42, 0.5) 0%, rgba(0, 67, 78, 0.5) 100%);
  border: 1px solid #57ffff;
  color: #ffffff;
  font-size: 10px;
  padding: 5px 20px;
  border-radius: 99px;
  cursor: pointer;
  margin-bottom: 10px;
}

.jkbox-err {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  color: #fff;
  text-align: center;
  line-height: 250px;
  background: rgba(0, 0, 0);
  z-index: 99999;
}

.video-container {
  width: 100%;
  height: 250px;
  margin: 0 auto;
  background: #000;
  position: relative;
}

/* 覆盖默认样式 */
:deep(.video-js) {
  width: 100%;
  height: 250px;
  position: relative;
}

:deep(.video-js .vjs-tech) {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

:deep(.vjs-big-play-button) {
  background-color: rgba(66, 184, 131, 0.7);
  border: none;
  width: 80px;
  height: 80px;
  line-height: 80px;
  border-radius: 50%;
  margin-left: -40px;
  margin-top: -40px;
}
</style>
